<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="链点名称" prop="cName">
        <el-input
          v-model="queryParams.cName"
          placeholder="请输入链点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链点地位" prop="cRole">
        <el-input
          v-model="queryParams.cRole"
          placeholder="请输入链点地位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="链点地位" prop="cRole">
        <el-input
          v-model="queryParams.cRole"
          placeholder="请输入链点地位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主营业务" prop="cBusiness">
        <el-input
          v-model="queryParams.cBusiness"
          placeholder="请输入主营业务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大可进口数" label-width="150px" prop="cMaximport">
        <el-input
          v-model="queryParams.cMaximport"
          placeholder="请输入最大可进口数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大可出口数" label-width="150px" prop="cMaxexport">
        <el-input
          v-model="queryParams.cMaxexport"
          placeholder="请输入最大可出口数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链点地址" prop="cAddress">
        <el-input
          v-model="queryParams.cAddress"
          placeholder="请输入链点地址"
          clearable
          @keyup.enter.native="handleQuery"
      </el-form-item>
        /> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
            <el-button type="primary" round  @click="openDialog">产业链模型预览</el-button>
    </el-form-item>
    </el-form>



    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">

        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['Resource:information:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['Resource:information:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['Resource:information:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="链点名称" align="center" prop="cName" />

      <el-table-column label="链点地位" align="center" prop="cRole" />
      <el-table-column label="主营业务" align="center" prop="cBusiness" />

      <el-table-column label="链点地址" align="center" prop="cAddress" />

      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['Resource:information:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['Resource:information:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产业链链点信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="链点名称" prop="cName">
          <el-input v-model="form.cName" placeholder="请输入链点名称" />
        </el-form-item>
        <el-form-item label="链点地位" prop="cRole">
          <el-input v-model="form.cRole" placeholder="请输入链点地位" />
        </el-form-item>
        <el-form-item label="主营业务" prop="cBusiness">
          <el-input v-model="form.cBusiness" placeholder="请输入主营业务" />
        </el-form-item>
        <el-form-item label="链点地址" prop="cAddress">
          <el-input v-model="form.cAddress" placeholder="请输入链表地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible="showDialog" @close="closeDialog" title="产业链模型">
      <div id="chainmodel" style="width: 1000px;height:1000px;"></div>
    </el-dialog>

  </div>
</template>

<script>
import { listInformation, getInformation, delInformation, addInformation, updateInformation } from "@/api/Resource/information";
import G6 from '@antv/g6';
import * as echarts from 'echarts';
var AData = ['欧洲矿业', '澳洲力拓集团'];
var BData = ['宁德时代', '潍柴动力','派能科技'];
var CData = ['理想汽车', '比亚迪'];
var DData = ['湖南销售处','广东销售处'];
var linkssss = [['欧洲矿业','宁德时代'],['澳洲力拓集团','宁德时代'],['欧洲矿业','潍柴动力'],['澳洲力拓集团','派能科技'],['宁德时代','理想汽车']
,['宁德时代','比亚迪'],['潍柴动力','比亚迪'],['派能科技','理想汽车'],['派能科技','比亚迪'],['比亚迪','湖南销售处'],['比亚迪','广东销售处'],
['理想汽车','湖南销售处']]

var legendData = ['原材料厂商', '供应商', '核心企业','销售商'];
var links = [];
var data1 = [],
  data2 = [],
  data3 = [];
var layout = 'none';
//第一层数据循环
for (var i = 0; i < AData.length; i++) {
  data1.push({
    name: AData[i],
    symbolSize: [30, 20],
    x: 0,
    y: i * (100 / AData.length),
    // value: 'ssss',
    category: 0,

  });
}
//第二层数据循环
for (var i = 0; i < BData.length; i++) {
  data2.push({
    name: BData[i],
    symbolSize: [30, 20],
    x: 30,
    y: i * (100 / BData.length),
    //value: [30, i * (100 / BData.length)],
    category: 1,
  });
}
//第三层数据循环
for (var i = 0; i < CData.length; i++) {
  data3.push({
    name: CData[i],
    symbolSize: [30, 20],
    x: 60,
    y: i * (100 / CData.length),
    value: [60, i * (100 / CData.length)],
    category: 2,
  });
}
//第四层数据循环
for (var i = 0; i < DData.length; i++) {
  data3.push({
    name: DData[i],
    symbolSize: [30, 20],
    x: 90,
    y: i * (100 / DData.length),
    // value: [60, i * (100 / lastData.length)],
    category: 3,
  });
}
//.....多层数据绑定出现时，采用双重循环绑定数据

//连接线数据建立
linkssss.forEach(item =>{
  links.push({
    source:item[0],
    label: {
      show: true,
      formatter: '{c}',
    },
    value:'',
    target:item[1],
  })
});
var datalist = data1.concat(data2, data3);

export default {

  name: "Information",
  data() {
    return {
      showDialog: false,
      graph: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产业链链点信息表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cName: null,
        cRole: null,
        cBusiness: null,
        cMaximport: null,
        cMaxexport: null,
        cAddress: null,
        status: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产业链链点信息列表 */
    getList() {
      this.loading = true;
      listInformation(this.queryParams).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cName: null,
        cId: null,
        cRole: null,
        cBusiness: null,
        cMaximport: null,
        cMaxexport: null,

        cAddress: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产业链链点信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cId = row.cId || this.ids
      getInformation(cId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产业链链点信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cId != null) {
            updateInformation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInformation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cIds = row.cId || this.ids;
      this.$modal.confirm('是否确认删除产业链链点信息编号为"' + cIds + '"的数据项？').then(function() {
        return delInformation(cIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/information/export', {
        ...this.queryParams
      }, `information_${new Date().getTime()}.xlsx`)
    },


    openDialog() {
      this.showDialog = true;
      this.$nextTick(() => {
        this.renderGraph();
      });
    },
    closeDialog() {
      this.showDialog = false;
      this.destroyGraph();
    },
    renderGraph() {

      var myChart = echarts.init(document.getElementById('chainmodel'));
      myChart.setOption({
    title: {
    text: '产业链模型',
    subtext: 'Default layout',
    top: 'bottom',
    left: 'right',
  },
  tooltip: {},
  legend: [
    {
      data: legendData,
    },
  ],
  animationDuration: 1500,
  animationEasingUpdate: 'quinticInOut',
  series: [
    {
      name: '产业链模型',
      type: 'graph',
      layout: layout, //布局方式：force引力布局
      draggable: true, //可拖动
      focusNodeAdjacency: true,
      roam: true,
      edgeSymbol: ['circle', 'arrow'],
      edgeSymbolSize: [0, 30],
      scaleLimit: {
            //所属组件的z分层，z值小的图形会被z值大的图形覆盖
            min: 0.5, //最小的缩放值
            max: 9 //最大的缩放值
        },
      label: {
        normal: {
          position: 'bottom',
          show: true,
          textStyle: {
            fontSize: 12,
          },
        },
      },
      lineStyle: {
        color: 'source',
        curveness: 0.3,
      },
      data: datalist,
      links: links,
      categories: [
        {
          name: '原材料厂商',
        },
        {
          name: '供应商',
        },
        {
          name: '核心企业',
        },
        {
          name: '销售商',
        },
      ],
    },
  ],
      });


    this.graph=myChart;

    },
    destroyGraph() {
      if (this.graph) {
        this.graph.destroy();
        this.graph = null;
      }
    },
  }

};
</script>
